import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @author zyj
 * @date 2022年05月23日 11:11
 */
public class HashSetDemo {
    public static void main(String[] args) {
        Set<Student> set = new HashSet<>();

        System.out.println(set.add(new Student("1", "张三", "1")));
        System.out.println(set.add(new Student("2", "李四", "2")));
        System.out.println(set.add(new Student("3", "王五", "3")));
        System.out.println(set.add(new Student("3", "王五", "4")));
        for (Student s: set) {
            System.out.println(s);
        }
    }
}

class Student implements Comparable<Student>{
    private String id;
    private String name;
    private String index;

    public Student(String id, String name,String index) {
        this.id = id;
        this.name = name;
        this.index = index;

    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIndex() {
        return index;
    }

    public void setIndex(String index) {
        this.index = index;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(id, student.id) && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id,name);
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", index='" + index + '\'' +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        if (getId().compareTo(o.getId()) > 0){
            return 1;
        }else if (getId().compareTo(o.getId()) < 0){
            return -1;
        }else {
            if (getName().compareTo(o.getName()) > 0) {
                return -1;
            } else if (getName().compareTo(o.getName()) < 0) {
                return 1;
            }
            return 0;
        }
    }
}
